#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <assert.h>

/*
 * 客户端向服务器发送信息，服务器接收并原样发送给客户端，客户端显示出接收到的信息
 */

#define IPADDR "127.0.0.1"
#define PORT 8787
#define MAXLINE 1024
#define LISTENQ 5
#define SIZE 10

typedef struct server_context_st
{
  int cli_cnt;	/*客户端个数*/
  int clifds[SIZE];/*客户端的个数*/
  fd_set allfds;	 /*句柄集合*/
  int maxfd;	/*句柄最大值*/
}server_context_st;

static server_context_st * s_srv_ctx = NULL;

static int create_server_proc(const char * ip, int port)
{
  int fd;
  int reuse;
  int yes;
  struct sockaddr_in servaddr;
  
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    fprintf(stderr, "create socket fail, erron:%d, reason:%s\n", errno, strerror(errno));
    return -1;
  }

  yes = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
    return -1;

  reuse = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) == -1)
    return -1;

  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  inet_pton(AF_INET, ip, & servaddr.sin_addr);
  servaddr.sin_port = htons(port);

  if (bind(fd, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1) {
    perror("bind error");
    return -1;
  }

  listen(fd, LISTENQ);

  return fd;
}

static int accept_client_proc(int srvfd)
{
  struct sockaddr_in cliaddr;
  socklen_t cliaddrlen;
  int clifd = -1;
  int i;

  cliaddrlen = sizeof(cliaddr);
  printf("accpet clint proc is called.\n");

 ACCEPT:
  clifd = accept(srvfd, (struct sockaddr *) & cliaddr, & cliaddrlen);
  if (clifd == -1) {
    if (errno == EINTR)
      goto ACCEPT;
    else {
      fprintf(stderr, "accept fail, error:%s\n", strerror(errno));
      return -1;
    }
  }

  fprintf(stdout, "accept a new client: %s:%d\n", inet_ntoa(cliaddr.sin_addr), cliaddr.sin_port);

  for (i = 0; i < SIZE; i++) {
    if (s_srv_ctx->clifds[i] < 0) {
      s_srv_ctx->clifds[i] = clifd;
      s_srv_ctx->cli_cnt++;
      break;
    }
  }

  if (i == SIZE) {
    fprintf(stderr, "too many clients.\n");
    return -1;
  }
}

static int handle_client_msg(int fd, char * buf)
{
  assert(buf);
  printf("recv buf is : %s\n", buf);
  write(fd, buf, strlen(buf) + 1);

  return 0;
}

static void recv_client_msg(fd_set * readfds)
{
  int i, n;
  int clifd;
  char buf[MAXLINE] = {0};

  for (i = 0; i <= s_srv_ctx->cli_cnt; i++) {
    clifd = s_srv_ctx->clifds[i];
    if (clifd < 0)
      continue;

    if (FD_ISSET(clifd, readfds)) {
		// 接收客户端发送的信息
      n = read(clifd, buf, MAXLINE);
      if (n <= 0) {
		FD_CLR(clifd, & s_srv_ctx->allfds);
		close(clifd);
		s_srv_ctx->clifds[i] = -1;
		continue;
      }

      handle_client_msg(clifd, buf);
    }
  }
}

static void handle_client_proc(int srvfd)
{
  int clifd = -1;
  int retval = 0;
  fd_set * readfds = & s_srv_ctx->allfds;
  struct timeval tv;
  int i;

  while (1) {
	  /*每次调用select前都要重新设置文件描述符和时间，因为事件发生后，文件描述符和时间都被内核修改啦*/
	  /*添加监听套接字*/

    FD_ZERO(readfds);
    FD_SET(srvfd, readfds);
    s_srv_ctx->maxfd = srvfd;

    tv.tv_sec = 30;
    tv.tv_usec = 0;

	/*添加客户端套接字*/
    for (i = 0; i < s_srv_ctx->cli_cnt; i++) {
      clifd = s_srv_ctx->clifds[i];
      if (clifd < 0)
		continue;
      FD_SET(clifd, readfds);
      s_srv_ctx->maxfd = (clifd > s_srv_ctx->maxfd ? clifd : s_srv_ctx->maxfd);
    }

    retval = select(s_srv_ctx->maxfd + 1, readfds, NULL, NULL, & tv);

    if (retval == -1) {
      fprintf(stderr, "select error:%s.\n", strerror(errno));
      return ;
    }

    if (retval == 0) {
      fprintf(stdout, "select is timeout.\n");
      continue;
    }

    if (FD_ISSET(srvfd, readfds))
	  accept_client_proc(srvfd); /*监听客户端请求*/
    else
      recv_client_msg(readfds);	/*接受处理客户端消息*/
  }
}

static void server_uninit()
{
  if (s_srv_ctx) {
    free(s_srv_ctx);
    s_srv_ctx = NULL;
  }
}

static int server_init()
{
  int i;

  s_srv_ctx = (server_context_st *) malloc (sizeof(server_context_st));
  if (s_srv_ctx == NULL)
    return -1;

  memset(s_srv_ctx, 0, sizeof(server_context_st));

  for (i = 0; i < SIZE; i++)
    s_srv_ctx->clifds[i] = -1;

  return 0;
}

int main(int argc, char * argv[])
{
  int srvfd;

  if (server_init() < 0)
    return -1;

  srvfd = create_server_proc(IPADDR, PORT);
  if (srvfd < 0) {
    fprintf(stderr, "socket create or bind fail.\n");
    goto err;
  }

  handle_client_proc(srvfd);

  return 0;

 err:
  server_uninit();
  return -1;
}
